package manager.action;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import manager.actionform.SuggestForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dao.SuggestMagDao;
import dao.impl.SuggestMagImpl;
import domain.Manager;
import domain.Student;
import domain.Suggest;
import domain.Teacher;

/**
 * 
 * DispatchAction，建议和BUGAction
 * 
 * May 10, 2009
 *
 * @author Fabian
 *
 */
public class ManagerSuggestAction extends DispatchAction {

	/**
	 * 浏览所有建议的Action，但是要传递分页参数（start--开始，limit--间隔）
	 */
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SuggestForm tf = (SuggestForm)form;
		int start = tf.getStart();
		int limit = tf.getLimit();		
		
		SuggestMagDao smd = new SuggestMagImpl();
		//获取此页面的建议
		List<Suggest> list = smd.getPageSuggest(start, limit);
		//获取总记录数
		int totalCount = smd.getTotalCount();
		
		//组装JSON字符串，返回
		String resultArray = "";
		for (Iterator<Suggest> it = list.iterator(); it.hasNext(); ) {
			Suggest s = it.next();
			Date d = s.getPubDate();
			long t = d.getTime();
			resultArray = resultArray + "{id: " + s.getId() + ", title: '" + s.getTitle() +
							"', sort: '" + s.getSort() + "', author: '" + s.getAuthor() +
							"', role: '" + s.getRole() + "', pubdate: new Date(" + t +
							"), content: '" + s.getContent() + "'}";
			if (it.hasNext()) {
				resultArray = resultArray + ",";
			}			
		}
		
		String jsonStr = "";
		jsonStr = "{totalProperty: " + totalCount + ", root: [ " + resultArray + " ]}";
		
		request.setAttribute("result", jsonStr);
				
		return mapping.findForward("show_result");
	}
	
	/**
	 * 删除建议，要传递建议的id
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SuggestForm tf = (SuggestForm)form;
		int del_id = tf.getId();
		
		//删除id对应建议
		SuggestMagDao smd = new SuggestMagImpl();
		boolean result = smd.delSuggest(del_id);
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{result: 'success', msg: '恭喜，删除建议成功！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{result: 'failure', msg: '抱歉，删除建议失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}		
		
		return mapping.findForward("del_result");
	}
	
	/**
	 * 添加建议，要传递建议的信息
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		SuggestForm tf = (SuggestForm)form;
		String title = tf.getTitle();
		int sort = tf.getSort_id();
		String content = tf.getContent();		
		
		//获取发布人的信息
		HttpSession session = request.getSession();
		Object user = session.getAttribute("user");
		int user_id = 1;
		int role = 3;
		if (user instanceof Manager) {
			Manager m = (Manager)user;
			user_id = m.getId();
			role = 1;
		} else if (user instanceof Teacher) {
			Teacher m = (Teacher)user;
			user_id = m.getId();
			role = 2;
		} else if (user instanceof Student) {
			Student m = (Student)user;
			user_id = m.getId();
			role = 3;
		}
		
		Suggest s = new Suggest();
		s.setTitle(title);
		s.setSort_id(sort);
		s.setContent(content);
		s.setUser_id(user_id);
		s.setRole_id(role);
		
		//删除id对应建议
		SuggestMagDao smd = new SuggestMagImpl();
		boolean result = smd.addSuggest(s);
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{success: true, msg: '恭喜，添加建议成功！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{failure: true, msg: '抱歉，添加建议失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}		
		return mapping.findForward("add_result");
	}
}
